<?php
class UserLanguagesController extends AppController {

	var $name = 'UserLanguages';
	var $components = array('RequestHandler');

	function index() {
		$this->UserLanguage->recursive = 0;
		$this->set('userLanguages', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid user language', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('userLanguage', $this->UserLanguage->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->UserLanguage->create();
			if ($this->UserLanguage->save($this->data)) {
				$this->Session->setFlash(__('The user language has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user language could not be saved. Please, try again.', true));
			}
		}
		$languages = $this->UserLanguage->Language->find('list');
		$levels = $this->UserLanguage->Level->find('list');
		$users = $this->UserLanguage->User->find('list');
		$this->set(compact('languages', 'levels', 'users'));
	}
	function ajax_add()
	{
		
		if ($this->RequestHandler->isAjax()) {
			if (!isset($this->cuid)) {
				echo "alert('错误！')";
				exit;
			}
			if (!empty($this->data)) {
				$exits=$this->UserLanguage->find('list',
				array('conditions'=>array('UserLanguage.user_id'=>$this->cuid,
				'UserLanguage.language_id'=>$this->data['UserLanguage']['language_id'])
				));
				if (empty($exits)) {
					$this->UserLanguage->create();
					if ($this->UserLanguage->save($this->data)) {
						$this->set('user_lan', $this->UserLanguage->findById($this->UserLanguage->id));
						$this->layout='ajax';
					}					
				}else{
					$this->autoRender=false;
					echo "<script>alert('esxits!');</script>";
				}

			}
		}
	}
	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid user language', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->UserLanguage->save($this->data)) {
				$this->Session->setFlash(__('The user language has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user language could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->UserLanguage->read(null, $id);
		}
		$languages = $this->UserLanguage->Language->find('list');
		$levels = $this->UserLanguage->Level->find('list');
		$users = $this->UserLanguage->User->find('list');
		$this->set(compact('languages', 'levels', 'users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for user language', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->UserLanguage->delete($id)) {
			$this->Session->setFlash(__('User language deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('User language was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>